﻿using Microsoft.Extensions.DependencyInjection;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Demo
{
    public interface IServer
    {
        IFeatureCollection Features { get; }
        
        Task StartAsync<TContext>(IHttpApplication<TContext> application, CancellationToken cancellationToken);
        
        Task StopAsync(CancellationToken cancellationToken);
    }

    public class TestServer : IServer
    {
        public TestServer()
        {
        }

        public IFeatureCollection Features => new FeatureCollection();

        Task IServer.StartAsync<TContext>(IHttpApplication<TContext> application, CancellationToken cancellationToken)
        {
            object context = new RequestContext("http://localhost:5000",Features);
            return application.ProcessRequestAsync((TContext)context);
        }

        Task IServer.StopAsync(CancellationToken cancellationToken)
        {
            return Task.CompletedTask;
        }
    }

    public static class FakeServerWebHostBuliderExtensions
    {
        public static IWebHostBuilder UseTestServer(this IWebHostBuilder builder)
        {
            return builder.ConfigureServices(services => services.AddSingleton<IServer, TestServer>());
        }
    }
}
